package com.qf.demo1.controller;

import com.qf.demo1.dao.CategoryDao;
import com.qf.demo1.entity.Category;
import com.qf.demo1.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/index")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @GetMapping("/cate")
    public HashMap<String,Object> selectAll(){

        List<Category> categories = categoryService.getAll();
        for (Category category : categories) {
                    List<Category> list = new ArrayList<>();
                    category.setCategories(list);
            for (Category category1 : categories) {
                if (category1.getParentId()==category.getCategoryId()){
                    Integer id = category1.getCategoryId();
//                    List<Category> category2 = categoryService.selectById(id);
//                    System.out.println(category2);
                    category.getCategories().add(category1);
                }
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",10000);
        map.put("msg","ok");
        map.put("data",categories);
        return map;

    }
}
